﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Mengsk.ProxyDelivery.Print.Service
{
    class VendorServiceWithCatch
    {
        private static Dictionary<string, Vendor> vendors = new Dictionary<string, Vendor>();
        private static VendorService vs = ServiceContainer.GetService<VendorService>();

        public static Vendor GetVendor(string vendorName)
        {
            lock (vendors)
            {
                if (vendors.ContainsKey(vendorName))
                {
                    return vendors[vendorName];
                }

                var vendor = vs.GetByName(vendorName);
                if (vendor == null || vendor.Count < 1)
                {
                    var tmpName = VendorService.FormatVendorName(vendorName);
                    vendor = vs.GetAllByFieldLike("name", tmpName, 0, 0).Datas;
                }

                if (vendor == null || vendor.Count < 1)
                {
                    return null;
                }

                if (vendor.Count > 1)
                {
                    MessageBox.Show("找到多个厂家：" + vendorName);
                }
                vendors.Add(vendorName, vendor[0]);
                return vendor[0];
            }
        }

        public static string GetVendorPingYingName(string vendorName)
        {
            var vendor = GetVendor(vendorName);
            if (vendor == null || string.IsNullOrWhiteSpace(vendor.PingyingName))
            {
                return "";
            }
            return vendor.PingyingName;
        }

        public static string GetVendorDoor(string vendorName)
        {
            var vendor = GetVendor(vendorName);
            if (vendor == null || string.IsNullOrWhiteSpace(vendor.MarketAddress))
            {
                return "";
            }
            return VendorService.FindDoor(vendor.MarketAddress);
        }

        public static string FormatVendorAreaAndDoor(String vendorName)
        {
            var vendor = GetVendor(vendorName);
            if (vendor == null || string.IsNullOrWhiteSpace(vendor.MarketAddress))
            {
                return "";
            }
            return string.Format("{0}-{1}", VendorService.FindAreaOrStreet(vendor.MarketAddress, "区"), VendorService.FindDoor(vendor.MarketAddress));
        }
    }
}
